//
//  HQRegisterViewController.m
//  MobileiOS
//
//  Created by 郝庆松 on 2018/5/5.
//  Copyright © 2018年 PCITC. All rights reserved.
//

#import "HQRegisterViewController.h"

@interface HQRegisterViewController ()

@end

@implementation HQRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _flg = NO;
    
    _yaoqingmaBg.hidden = YES;
    _yaoqignmaConstraint.constant = 0;
    
    [self setPlaceHolderColor:_userName];
    [self setPlaceHolderColor:_phoneNo];
    [self setPlaceHolderColor:_password];
    [self setPlaceHolderColor:_yaoqingMark];
    [self addKeyBoardObserver];
}

/**
 隐私协议
 
 @param sender 按钮
 */
- (IBAction)yinsixieyi:(id)sender {
    HQYinSiXieYiViewController *yinsixieyi = [[HQYinSiXieYiViewController alloc]init];
    [self presentViewController:yinsixieyi animated:YES completion:nil];
}

//当键盘出现或改变时调用
- (void)keyboardWillShow:(NSNotification *)aNotification{
    NSDictionary *userInfo = [aNotification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    _viewHeightConstraints.constant = 680 + keyboardRect.size.height;
}

//当键退出时调用
- (void)keyboardWillHide:(NSNotification *)aNotification{
    _viewHeightConstraints.constant = 680;
}

- (IBAction)bossItem:(id)sender {
    _type = 1;
    _yaoqingmaBg.hidden = YES;
    _yaoqignmaConstraint.constant = 0;
    [_bossItem setTitleColor:[self colorHexValue:0x63ADEA] forState:UIControlStateNormal];
    _bossItem.backgroundColor = [UIColor whiteColor];
    [_employeeItem setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _employeeItem.backgroundColor = [UIColor clearColor];
    _postName.text = @"厂家";
}

- (IBAction)employeeItem:(id)sender {
    _yaoqingmaBg.hidden = NO;
    _yaoqignmaConstraint.constant = 38;
    _type = 2;
    _postName.text = @"零售商";
    [_bossItem setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _bossItem.backgroundColor = [UIColor clearColor];
    [_employeeItem setTitleColor:[self colorHexValue:0x63ADEA] forState:UIControlStateNormal];
    _employeeItem.backgroundColor = [UIColor whiteColor];
}

/**
 注册账号
 
 @param sender 注册账号
 */
- (IBAction)registerItem:(id)sender {
    if([_selectArea.titleLabel.text isEqualToString:@"请选择地区"]){
        [self tostView:@"请选择地区"];
        return;
    }
    if(_type == 0){
        [self tostView:@"请选择职位"];
        return;
    }
    if([self isInputEmpty:_userName.text]){
        [self tostView:@"请输入用户名称"];
        return;
    }
    if([self isInputEmpty:_phoneNo.text]){
        [self tostView:@"请输入手机号"];
        return;
    }
    if(![HQTool phoneNoRegex:_phoneNo.text]){
        [self tostView:@"请输入正确的手机号"];
        return;
    }
    if([self isInputEmpty:_password.text]){
        [self tostView:@"请输入密码"];
        return;
    }
    if(!_flg){
        [self tostView:@"请同意软件许可协议"];
        return;
    }
    NSString *yqm = _yaoqingMark.text == nil ? @"":_yaoqingMark.text;
    [self showHudText:@""];
    [HQNetWork requetParam:[HQParam registerUser_name:_userName.text password:_password.text state:[NSString stringWithFormat:@"%ld",(long)_type] phone:_phoneNo.text prov_id:_prov_id prov_txt:_prov_txt city_id:_city_id city_txt:_city_txt code:yqm] withMethod:@"user/register" suc:^(NSDictionary *sucData) {
        [self hiddnHud];
        if([sucData[kStatus] integerValue]== 1){
            UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"恭喜您，注册成功" message:@"请拨打人扑客服热线申请开通\n010 8622 0305" preferredStyle:UIAlertControllerStyleAlert];
            [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            }]];
            [alertController addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                [self dismissViewControllerAnimated:YES completion:^{}];
            }]];
            [self presentViewController:alertController animated:YES completion:nil];
        }else{
            [self tostView:sucData[kInfo]];
        }
    } fail:^{
        [self hiddenErrorHud];
    }];
}

/**
 关闭当前界面
 
 @param sender 关闭按钮
 */
- (IBAction)coloseItem:(id)sender {
    [self dismissViewControllerAnimated:YES completion:^{}];
}

/**
 选择区域
 
 @param sender 按钮
 */
- (IBAction)selectAreaItem:(id)sender {
    [self showHudText:@""];
    [HQNetWork requetParam:[HQParam getarea:@"0"] withMethod:[HQMethod getarea] suc:^(NSDictionary *sucData) {
        [self hiddnHud];
        if([sucData[kStatus] integerValue]== 1){
            _provArray = sucData[@"data"];
            NSMutableArray *m = [[NSMutableArray alloc]init];
            for(NSDictionary *dict in _provArray){
                [m addObject:[NSString stringWithFormat:@"%@",dict[@"haa_name"]]];
            }
            ZHPickView *zhPickerView = [[ZHPickView alloc]initPickviewWithArray:m isHaveNavControler:NO];
            [zhPickerView setPickeTitle:@"设置省份"];
            [zhPickerView selectObj:_prov_txt];
            zhPickerView.delegate = self;
            zhPickerView.tag = 2001;
            [zhPickerView show];
        }else{
            [self tostView:sucData[kInfo]];
        }
    } fail:^{
        [self hiddenErrorHud];
    }];
}

-(void)toobarDonBtnHaveClick:(ZHPickView *)pickView resultString:(NSString *)resultString{
    if(pickView.tag == 2001){
        tempProv_txt = resultString;
        for(NSDictionary *dict in _provArray){
            if([dict[@"haa_name"] isEqualToString:resultString]){
                tempProv_id = dict[@"haa_id"];
                break;
            }
        }
        [self showHudText:@""];
        [HQNetWork requetParam:[HQParam getarea:tempProv_id] withMethod:[HQMethod getarea] suc:^(NSDictionary *sucData) {
            [self hiddnHud];
            if([sucData[kStatus] integerValue]== 1){
                _cityArray = sucData[@"data"];
                NSMutableArray *m = [[NSMutableArray alloc]init];
                for(NSDictionary *dict in _cityArray){
                    [m addObject:[NSString stringWithFormat:@"%@",dict[@"haa_name"]]];
                }
                ZHPickView *zhPickerView = [[ZHPickView alloc]initPickviewWithArray:m isHaveNavControler:NO];
                [zhPickerView setPickeTitle:@"设置城市"];
                zhPickerView.delegate = self;
                zhPickerView.tag = 2002;
                [zhPickerView show];
            }else{
                [self tostView:sucData[kInfo]];
            }
        } fail:^{
            [self hiddenErrorHud];
        }];
    }else if(pickView.tag == 2002){
        _city_txt = resultString;
        for(NSDictionary *dict in _cityArray){
            if([dict[@"haa_name"] isEqualToString:resultString]){
                _city_id = dict[@"haa_id"];
                break;
            }
        }
        _prov_id = tempProv_id;
        _prov_txt = tempProv_txt;
        [_selectArea setTitle:[NSString stringWithFormat:@"%@ %@",_prov_txt,_city_txt] forState:UIControlStateNormal];
    }
}
- (IBAction)checkMark:(id)sender {
    _flg = !_flg;
    if(_flg){
        [_checkMark setBackgroundImage:[UIImage imageNamed:@"register_sel"] forState:UIControlStateNormal];
    }else{
        [_checkMark setBackgroundImage:[UIImage imageNamed:@"register_def"] forState:UIControlStateNormal];
    }
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if(scrollView.contentOffset.y <-50){
        [self.view endEditing:YES];
    }
}

@end
